package com.xz.group12306.business.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.xz.group12306.business.common.DBUtils;
import com.xz.group12306.business.common.ServiceException;
import com.xz.group12306.business.dao.UserDao;
import com.xz.group12306.business.dao.impl.UserDaoImpl;
import com.xz.group12306.business.service.UserService;
import com.xz.group12306.domain.Cart;
import com.xz.group12306.domain.User;

public class UserServiceImpl implements UserService{

	
	
	/**
	 * @author 胡晨倩
	 */
	@Override
	public User login(String username, String password, String rule) {
		Connection conn = null;
		conn=DBUtils.getConnection();
		DBUtils.beginTransaction(conn);
		// TODO Auto-generated method stub
		UserDaoImpl userDaoImpl = new UserDaoImpl(conn);// 实例化UserDao的实现类
		User user=userDaoImpl.selectUser(username,password, rule);
		DBUtils.closeConnection(conn);
		
		return user;
	}

	/**
	 * @author 沈姚楠
	 */
	public void addUser(User user) {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			UserDaoImpl userDaoImpl = new UserDaoImpl(conn);
			try {
			DBUtils.beginTransaction(conn);
		
			userDaoImpl.save(user);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			DBUtils.commit(conn);
		} finally {
			DBUtils.closeConnection(conn);
		}
	}

	
	/**
	 * @author 沈姚楠
	 */
	//查询所有的用户
	@Override
	public User getAllUser(User one) {
		// TODO Auto-generated method stub
		User user=null;
		Connection conn=null;
		conn=DBUtils.getConnection();
		UserDao userDao=new UserDaoImpl(conn);
		DBUtils.beginTransaction(conn);
		try {
			user=userDao.allUser(one);
			DBUtils.commit(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			DBUtils.rollback(conn);
			throw new ServiceException("查询错误", e);
		}finally {
			DBUtils.closeConnection(conn);
		}
		return user;
	}

	
	/**
	 * @author 胡晨倩
	 */
	@Override
	public void updatePassword(String password, int id) {
		// TODO Auto-generated method stub
		Connection conn = null;
		conn=DBUtils.getConnection();
		DBUtils.beginTransaction(conn);
		// TODO Auto-generated method stub
		UserDaoImpl userDaoImpl = new UserDaoImpl(conn);// 实例化UserDao的实现类
		userDaoImpl.updatePwd(password, id);
		DBUtils.commit(conn);
		DBUtils.closeConnection(conn);
	}

	/**
	 * @author 胡晨倩
	 */
	@Override
	public List<Cart> findCartByUser(User user, int page, int rows) {
		// TODO Auto-generated method stub
		Connection conn = null;
		List<Cart> carts = null;
		try {
		conn = DBUtils.getConnection();
		UserDao userDao = new UserDaoImpl(conn);
		DBUtils.beginTransaction(conn);
		carts = userDao.findCartByUser(user, page, rows);
		DBUtils.commit(conn);
		} catch (SQLException e) {
			DBUtils.rollback(conn);
		try {
		throw new ServiceException("查询错误", e);
		} catch (ServiceException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
		}
		} finally {
			DBUtils.closeConnection(conn);
		}

		return carts;
	}

	/**
	 * @author 沈姚楠
	 */
	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		Connection conn=null;
		try {
			
		
		conn=DBUtils.getConnection();
		UserDao userDao=new UserDaoImpl(conn);
		DBUtils.beginTransaction(conn);
		userDao.updateUser(user);
		DBUtils.commit(conn);
		} catch (Exception e) {
			// TODO: handle exception
			DBUtils.rollback(conn);
		}finally {
			DBUtils.closeConnection(conn);
		}
	}


}
